﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FixedPointNormalVector
{
    public partial class Form1 : Form
    {
        Point p = new Point();
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String []s1=PointA.Text.Split(',');
            p.Ax = double.Parse(s1[0]);
            p.Ay = double.Parse(s1[1]);
            p.Az = double.Parse(s1[2]);
            String[] s2 = PointB.Text.Split(',');
            p.Bx = double.Parse(s2[0]);
            p.By = double.Parse(s2[1]);
            p.Bz = double.Parse(s2[2]);
            String[] s3 = PointC.Text.Split(',');
            p.Cx = double.Parse(s3[0]);
            p.Cy = double.Parse(s3[1]);
            p.Cz = double.Parse(s3[2]);
            //向量AB坐标(Bx-Ax,By-Ay,Bz-Az)
            double ab1 = p.Bx - p.Ax; double ab2 = p.By - p.Ay; double ab3 = p.Bz - p.Az;
            double ac1 = p.Cx - p.Ax; double ac2 = p.Cy - p.Ay; double ac3 = p.Cz - p.Az;
            double bc1 = p.Cx - p.Bx; double bc2 = p.Cy - p.By; double bc3 = p.Cz - p.Bz;
            if (((ab1*ac2==ac1*ab2) && (ab2*ac3==ac2*ab3))
                || (ab1*bc2==bc1*ab2 && ab2*bc3==bc2*ab3)
                )
            {
                label4.Text = p.By.ToString();
            }
            else
            {
                //向量AB和AC的叉积算出法向量
                double x = ab1 * ac1;
                double y = ab2 * ac2;
                double z = ab3 * ac3;
                //算出法向量的长度l
                double l = Math.Abs(Math.Sqrt(Math.Pow(x, 2.0) + Math.Pow(y, 2.0) + Math.Pow(z, 2.0)));
                //单位法向量的坐标等于法向量的坐标除以法向量的长度；
                double a = x / l;
                double b = y / l;
                double c = z / l;
                UnitNormalVector.Text = a.ToString() + ';' + b.ToString() + ';' + c.ToString();
            }
                

        }

        private void PointA_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和分号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(';'))
            {

                e.Handled = true;
            }


            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为分号*/
            if (e.KeyChar == (char)(';') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(';') != 1)
            {
                e.Handled = true;
            }
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和分号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(','))
            {

                e.Handled = true;
            }


            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为分号*/
            if (e.KeyChar == (char)(',') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(';') != 1)
            {
                e.Handled = true;
            }
        }

        private void PointC_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和逗号 
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(','))
            {

                e.Handled = true;
            }


            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为分号*/
            if (e.KeyChar == (char)(',') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(',') != 1)
            {
                e.Handled = true;
            }
        }
    }
}
